//
//  LanguageSelectView.m
//  StormWatch
//
//  Created by 王岩 on 2024/8/30.
//

#import "LanguageSelectView.h"
#import "JYPopContainerView.h"
#import "JYRoot.h"
#import "YYCategoriesMacro.h"
#import "LanguageSelectViewCell.h"

@interface LanguageSelectView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, weak) JYPopContainerView *popView;
/* <#note#> */
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSArray * iconArray, * titleArray;
@property (nonatomic, assign) NSInteger currentIndex;

@end
@implementation LanguageSelectView

- (instancetype)init {
    if (self = [super init]) {
        
        self.titleArray = @[@"中文", @"English", @"español", @"PoccИйckag ФeдepaциЯ", @"한국어", @"日本語", @"Türkçe", @"Deutsch",@"Français",@"Italiano",@"Português",@"Lingua Latina"];
        self.iconArray = @[@"icon_language_chinese",  @"icon_language_english", @"icon_language_spain", @"icon_language_russia", @"icon_language_korea", @"icon_language_japan", @"icon_language_turkey", @"icon_language_germany",@"法国",@"意大利",@"葡萄牙",@"拉丁语"];
        
        self.currentIndex = [U_UserDefaults fetchLanguage].integerValue;
        
        [self setupUI];
        [self bar];
        self.bar.titleLab.text = SWString(@"Choose language");
    }
    return self;
}

- (void)setupUI {
   
    [self addSubview:self.collectionView];

    UIButton * sureBtn = [SWInitializer jy_initButtonWithTarget:self action:@selector(changeLanguageAction)  title:SWString(@"Confirm")
                                                     titleColor:[UIColor whiteColor]
                                                           font:S_bfont(16)
                                                        bgColor:S_HexColor(@"#041A72")  on:self layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(10);
        kMakeRV(-10);
        kMakeBV(-klSafeAreaEdgeInsets.bottom - 10);
        kMakeHV(50);
    }];
    sureBtn.layer.cornerRadius = 10;
       
}

- (void)changeLanguageAction {
    
    [U_UserDefaults saveLanguage:[NSString stringWithFormat:@"%ld", self.currentIndex]];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_theme" object:nil];
    
    [self dimiss];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.iconArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LanguageSelectViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LanguageSelectViewCell" forIndexPath:indexPath];
    
    cell.countryIcon.image = S_imageNamed(self.iconArray[indexPath.row]);
    cell.languageLab.text = self.titleArray[indexPath.row];
    if (self.currentIndex == indexPath.row + 1) {
        cell.layer.borderColor = S_HexColor(@"#F57A36").CGColor;
        cell.checkImg.image = S_imageNamed(@"item_language_selected_icon");
    }else {
        cell.layer.borderColor = S_HexColor(@"#eeeeee").CGColor;
        cell.checkImg.image = S_imageNamed(@"item_language_unselect_icon");
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.currentIndex = indexPath.row + 1;
    [self.collectionView reloadData];
}

- (void)dimiss
{
    [self.popView dissmiss];
}
- (void)show
{
    if (self.popView) {
        [self.popView removeFromSuperview];
    }
    JYPopContainerView *popView = [JYRoot popBottomView:self animation:YES maker:^(MASConstraintMaker * _Nonnull make) {
        kMakeLRV(0);
        kMakeBV(0);
        kMakeHV(klSafeAreaEdgeInsets.bottom  + 600);
    } block:nil];
    [popView layoutSubviews];
    _popView = popView;

    @weakify(popView);
    self.eventTransmissionBlock = ^(id  _Nonnull params, NSInteger tag) {
        @strongify(popView);
        if (tag == 0) {
            [popView dissmiss];
        }
    };

    [self.superview layoutIfNeeded];

}


- (NSString *)containerColor {
    return @"#FFFFFF";
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kScreenWidth - 20, 55);
        layout.minimumLineSpacing = 10;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 600 - 70) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"LanguageSelectViewCell" bundle:nil] forCellWithReuseIdentifier:@"LanguageSelectViewCell"];
    }
    return _collectionView;
}

@end
